<?php
/**
 * Minhlog Domain_Mapper 文章分类类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_DataMapper
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Category.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_DataMapper_Category extends Domain_DataMapper_Abstract
{
    protected $_tableClassName = 'Domain_Table_Category';

    protected $_dataMapper = array (
            'categoryId'   => 'category_id',
            'parentId'     => 'parent_id',
            'categoryName' => 'category_name',
            'categoryDesc' => 'category_desc',
            'displayOrder' => 'display_order'
    );

    /**
     * 根据父类查询子类数据
     * @param integer $parentId
     * @return object|void
     */
    public function findChildCategoryByParent($parentId)
    {
        return $this->getTable()->fetchAll($this->findCategoryTreeStatement($parentId));
    }

    /**
     * 查询根分类树数据
     * @param integer $rootId
     * @return object|void
     */
    public function findRootCategoryTree()
    {
        return $this->getTable()->fetchAll($this->findCategoryTreeStatement(0));

    }

    /**
     * 根据父分类查询子分类树
     * @param Zend_Db_Table_Rowset $linkType
     * @return object|void
     */
    public function findChildCategoryTree(Zend_Db_Table_Rowset $parentCategorySet)
    {
        if($parentCategorySet->count()){
            $categorySet = array();
            foreach($parentCategorySet as $parent){
                $childSet = $parent->findDependentRowset('Domain_Table_Category','Parent',$this->findCategoryTreeStatement($parent->category_id));
                if($childSet->count()){
                    foreach($childSet as $child){
                        $childCategory = $this->mapper($child);
                        $categorySet[$parent->category_id][] = $childCategory;
                    }
                }
            }
            $resultSet = $categorySet;
        }else{
            $resultSet = $parentCategorySet;
        }
        return $resultSet;
    }

    /**
     * 根据文章查询分类
     * @param Zend_Db_Table_Row $article
     * @return object|void
     */
    public function findCategoryByArticle(Zend_Db_Table_Row $article)
    {
        if(null !== $article){
            $category = $this->mapper($article->findParentRow('Domain_Table_Category','Category'));
            $result = $category;
        }else{
            $result = null;
        }
        return $result;
    }

    /**
     * 查询全部分类信息
     * @param null|integer $parentId
     * @return object|void
     */
    public function findAllCategory($parentId = null)
    {
        return $this->findAll($this->findAllCategoryStatement($parentId));
    }

    /**
     * 根据多篇文章查询分类
     * @param Zend_Db_Table_Rowset $articleSet
     * @return object|void
     */
    public function findCategoryByArticles(Zend_Db_Table_Rowset $articleSet)
    {
        if($articleSet->count()){
            $categorys = array();
            foreach($articleSet as $article){
                $category = $this->mapper($article->findParentRow('Domain_Table_Category','Category'));
                $categorys[$article->article_id] = $category;
            }
            $resultSet = $categorys;
        }else{
            $resultSet = null;
        }
        return $resultSet;
    }

    /**
     * 封装查询分类树信息SQL对象
     * @param integer $nodeId
     * @return Zend_Db_Table_Select
     */
    private function findCategoryTreeStatement($nodeId)
    {
        $db = $this->getTable()->getAdapter();
        $select = $this->getTable()->select();
        $select->where($db->quoteInto('parent_id = ?', $nodeId))
                ->order('display_order ASC')
                ->order('category_id DESC');
        return $select;
    }

    /**
     * 封装查询全部分类信息SQL对象
     * @param integer $parentId
     * @return Zend_Db_Table_Select
     */
    private function findAllCategoryStatement($parentId = null)
    {
        $select = $this->getTable()->select();
        if(null !== $parentId) $select->where('parent_id = ?',$parentId);
        $select->order('category_id DESC');
        return $select;
    }

}